﻿using System;
using System.Drawing;
using System.Collections.Generic;
using System.Windows.Forms;
using Microsoft.Win32;
using posmoon.Forms;
using posmoon.Tree;

namespace posmoon
{
    public partial class MainForm : Form
    {
        private HostRegisterForm frmRegHost;
        private DBRegisterForm frmRegDB;
        private List<HostTreeNode> _listOfHost;
        
        public MainForm()
        {
            InitializeComponent();
            _listOfHost = new List<HostTreeNode>();
            mnuItemConn.Click += ConnectToDB;
            treeView.DoubleClick += ConnectToDB;            
        }

        private void mnuAbout_Click(object sender, EventArgs e)
        {
            
        }

        private void smnuRegHost_Click(object sender, EventArgs e)
        {
            frmRegHost = new HostRegisterForm();
            frmRegHost.ShowDialog();

            _listOfHost.Add(frmRegHost.NewNode);

            addNewNode(frmRegHost.NewNode);
        }

        private void smnuRegDB_Click(object sender, EventArgs e)
        {
            HostTreeNode node;

            if (treeView.Nodes[0].IsSelected)
                return;

            node = _listOfHost.Find(item => item == treeView.SelectedNode);

            frmRegDB = new DBRegisterForm(node);
            frmRegDB.ShowDialog();
            
            foreach (DBTreeNode dbnode in frmRegDB.GetDB)
            {
                dbnode.ContextMenuStrip = mnuTreeHost;
                dbnode.ContextMenuStrip.Items[3].Enabled = false;
                dbnode.ImageIndex = 2;
                dbnode.SelectedImageIndex = 2;

                treeView.SelectedNode.Nodes.Add(dbnode);               
            }
            treeView.SelectedNode.ExpandAll(); 
        }

        private void treeView_AfterSelect(object sender, TreeViewEventArgs e)
        {

        }

        private void addNewNode(HostTreeNode node)
        {
            node.ContextMenuStrip = mnuTreeHost;            
            node.ImageIndex = 1;
            node.SelectedImageIndex = 1;
            node.Text = node.HostName + "(" + node.HostIP + ")";
            treeView.Nodes[0].Nodes.Add(node);
            treeView.Nodes[0].ExpandAll();
            treeView.SelectedNode = node;
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            RegistryKey k = Registry.CurrentUser.OpenSubKey("Software\\posmoon\\", false);
            if (k != null)
            foreach (string keyname in k.GetSubKeyNames())
            {
                RegistryKey key = k.OpenSubKey(keyname, false);
                HostTreeNode node = new HostTreeNode(key);
                node.ReadFromRegistry();
                node.HostName = keyname;
                addNewNode(node);
                _listOfHost.Add(node);
            }

            treeView.SelectedNode = treeView.Nodes[0];
        }

        private void ConnectToDB(object sender, EventArgs e)
        {
            HostTreeNode node;

            if (treeView.Nodes[0].IsSelected)
                return;            

            node = _listOfHost.Find(item => item == treeView.SelectedNode);

            node.openConnection();            

            if (node.Connected)
            {
                treeView.SelectedNode.ForeColor = SystemColors.WindowText;

                treeView.SelectedNode.NodeFont = new Font("", 8.25F , FontStyle.Bold);

                mnuItemConn.Enabled = false;
                mnuItemDConn.Enabled = true;
                mnuItemProp.Enabled = true;
            }
        }
    }
}
